<script setup>
import { ref, reactive } from "vue";
import { useRouter } from "vue-router";
import { useUserStore } from "@/store/modules/useUserStore.js";
import { ElMessage } from "element-plus";

// 用户登录
const loading = ref(false);
const router = useRouter();
const userInfoStore = useUserStore();

const loginform = reactive({
  self_account: null,
  password: null
});

// 密码验证规则
const validPass = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入密码！"));
  }
  callback();
};
// 用户名验证规则
const validnName = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入用户名！"));
  }
  callback();
};
const rules = reactive({
  self_account: [{ validator: validnName, trigger: "blur" }],
  password: [{ validator: validPass, trigger: "blur" }]
});

const loginFormRef = ref();
const loginSubmit = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      loading.value = true;
      try {
        const flag = await userInfoStore.userLoginInit(loginform);
        if (flag) {
          router.replace({ path: "/Home" });
          ElMessage.success("登录成功！");
          loading.value = false;
        } else {
          loading.value = false;
          ElMessage.error("登录失败！");
        }
      } catch (error) {
        loading.value = false;
        console.log(error);
      }
    } else {
      console.log("error", fields);
    }
  });
};

const merryPass = ref(false);
</script>
<template>
  <div>
    <div class="login-card">
      <div class="card-header">
        <div>欢迎使用</div>
        <div>Himate 后台管理系统</div>
      </div>
      <div class="card-content">
        <el-form label-position="top" label-width="100px " :model="loginform" ref="loginFormRef" :rules="rules" style="max-width: 460px">
          <el-form-item label="账号" prop="Account">
            <el-input v-model="loginform.self_account" placeholder="请输入用户名" clearable />
          </el-form-item>
          <el-form-item label="密码" prop="Password">
            <el-input v-model="loginform.password" placeholder="请输入用户密码" type="password" show-password clearable />
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="merryPass" label="记住密码" size="large" />
          </el-form-item>
        </el-form>
        <el-button type="primary" :loading="loading" @click="loginSubmit(loginFormRef)">立即登录</el-button>
        <!-- <div class="lose-pass" @click="LosePass()">忘记密码？</div> -->
      </div>
      <div class="card-footer">
        <div class="left-line"></div>
        <div class="name">Welcome to Himate</div>
        <div class="right-line"></div>
      </div>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.login-card {
  width: 456px;
  height: 551px;
  background: #ffffff;
  border-radius: 12px 12px 12px 12px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  box-sizing: border-box;
  padding-top: 48px;

  .card-header {
    width: 376px;
    height: 64px;
    font-size: 24px;
    font-weight: 600;
    color: #333333;
  }

  .card-content {
    width: 376px;
    height: 342px;

    :deep(.ez-form) {
      .ez-input {
        width: 376px;
        height: 40px;
        background: #ffffff;
        //border-radius:8px 8px 8px 8px ;
      }

      .ez-input__wrapper {
        border-radius: 8px 8px 8px 8px;
        border: 1px solid #eeeeee;
        box-shadow: none;
      }

      .is-focus {
        box-shadow: 0 0 0 1px var(--ez-color-primary);
      }
    }

    .ez-button {
      width: 376px;
      height: 40px;
      border-radius: 8px 8px 8px 8px;
      font-size: 14px;
      color: #ffffff;
    }

    .lose-pass {
      margin-top: 16px;
      width: 80px;
      height: 21px;
      font-size: 16px;
      font-weight: 400;
      color: #666666;
    }
  }

  .card-footer {
    width: 376px;
    height: 19px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .left-line {
      width: 100px;
      height: 1px;
      background: #eeeeee;
    }

    .right-line {
      width: 100px;
      height: 1px;
      background: #eeeeee;
    }

    .name {
      font-size: 14px;
      font-weight: 400;
      color: #cccccc;
    }
  }
}
</style>
